package com.qidian.mes.tm.service.Impl;

import com.qidian.mes.pojo.Tool;
import com.qidian.mes.pojo.ToolType;
import com.qidian.mes.tm.mapper.ToolMapper;
import com.qidian.mes.tm.service.ToolService;
import com.qidian.mes.util.dot.PagerDTO;
import com.qidian.mes.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 陆雯城
 * @Description
 * @Time 2024-04-08 21:02
 */
@Service
public class ToolServiceImpl implements ToolService {
    @Autowired
    private ToolMapper toolMapper;


    @Override
    public QdResult deleteByToolIds(Long[] toolIds) {
        toolMapper.deleteByToolIds(toolIds);
        return QdResult.success();
    }

    @Override
    public QdResult deleteByToolId(Long toolId) {
        toolMapper.deleteByToolId(toolId);
        return QdResult.success();
    }

    @Override
    public QdResult updateTool(Tool tool) {
        if (tool.getToolId() == null){
            return QdResult.error("ToolId is null");
        }
        Tool tempTool = toolMapper.selectByToolCode(tool.getToolCode());
        if (tempTool != null && !tempTool.getToolId().equals(tool.getToolId())){
            return QdResult.duplicate("duplicate: toolCode");
        }
        tempTool = toolMapper.selectByToolName(tool.getToolName());
        if (tempTool != null && !tempTool.getToolId().equals(tool.getToolId())){
            return QdResult.duplicate("duplicate: toolName");
        }
        return  QdResult.isSuccess(toolMapper.updateTool(tool));
    }

    @Override
    public QdResult queryByToolId(Long toolId) {
        return QdResult.success(toolMapper.selectByToolId(toolId));
    }

    @Override
    public QdResult saveTool(Tool tool) {
        if (toolMapper.selectByToolName(tool.getToolName()) != null) {
            return QdResult.duplicate("duplicate: toolName");
        }
        if (toolMapper.selectByToolCode(tool.getToolCode()) != null) {
            return QdResult.duplicate("duplicate: toolCode");
        }
        return QdResult.isSuccess(toolMapper.insertTool(tool));
    }

    @Override
    public QdResult<PagerDTO<Tool>> queryByPage(Tool tool, int page, int limit) {
        PagerDTO<Tool> pagerDTO = new PagerDTO<>();
        pagerDTO.setPage(page);
        pagerDTO.setLimit(limit);
        pagerDTO.setTotal(toolMapper.selectCount(tool));
        pagerDTO.setData(toolMapper.selectByPage(tool, (page - 1) * limit, limit));
        return QdResult.success(pagerDTO);
    }
}
